﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.ApplicationPages;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.SharePoint.Utilities;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.Collections.Generic;
using System.Collections;

namespace SpugPt.DataConnManager
{
    /// <summary>
    /// SpugPtDataConnManager
    /// </summary>
    public partial class SpugPtDataConnManager : GlobalAdminPageBase
    {
       
        private DataTable _dt;       
        public String WebId { get; set; }
        protected ToolBarButton _btCreateKey;
        SPWebApplication _webApp;

        /// <summary>
        /// Raises the <see cref="E:Load"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            WebId = String.Empty;
            base.OnLoad(e);
            Selector.ContextChange += new EventHandler(Selector_ContextChange);
        }
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            _btCreateKey = (ToolBarButton)idAddField;
            _dt = Helper.CreateDataTable();
            GetPropValues();            
        }
        
        /// <summary>
        /// Raises the <see cref="E:PreRender"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected override void OnPreRender(EventArgs e)
        {
            _btCreateKey.Visible = IsWebAppChosen();
            if (_btCreateKey.Visible) _btCreateKey.NavigateUrl = String.Format("javascript:openPopup('{0}','','')",WebId);
            base.OnPreRender(e);
        }

        /// <summary>
        /// Handles the ContextChange event of the Selector control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void Selector_ContextChange(object sender, EventArgs e)
        {
            //user changed the web application selection
            // NOTE: this event also fire when the page first loads
            var wa = ((WebApplicationSelector)sender).CurrentItem;
            if (wa == null)
            {
                return;
            }
            _webApp = wa;
            WebId = wa.Id.ToString();
            GetPropValues();


        }
        /// <summary>
        /// Gets the link.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        /// <param name="operation">The operation.</param>
        /// <returns></returns>
        protected string GetLink(string name,string value, string operation)
        {
            StringBuilder url = new StringBuilder();
            url.Append(String.Concat("'", WebId,"',"));
            url.Append(String.Concat("'", name , "',"));
            url.Append(String.Concat("'", value , "',"));
            url.Append(String.Concat("'", operation, "'"));
            string popUp = string.Concat("javascript:openPopup(" ,url.ToString() ,")");
            String tmp = String.Concat("<a href=\"", popUp, "\">", name, "</a>");
            return tmp;
        }
        /// <summary>
        /// Gets the values on the form.
        /// </summary>
        private void GetPropValues()
        {

            if (_webApp != null)
            {
                foreach (DictionaryEntry Item in _webApp.Properties)
                {
                    if (Item.Key.ToString().StartsWith(Helper.PREFIX))
                    {

                        DataRow dr = _dt.NewRow();
                        dr["Key"] = Item.Key.ToString();
                        dr["Name"] = Item.Key.ToString().Replace(Helper.PREFIX, "");
                        dr["Value"] = Item.Value.ToString();
                        _dt.Rows.Add(dr);

                    }

                }
            }
            _dt.DefaultView.Sort = "Name";
            DgItems.DataSource = _dt.DefaultView;
            DgItems.DataBind();

        }

       

        /// <summary>
        /// Determines whether [is web app chosen].
        /// </summary>
        /// <returns>
        ///   <c>true</c> if [is web app chosen]; otherwise, <c>false</c>.
        /// </returns>
        private bool IsWebAppChosen()
        {
            return (WebId != String.Empty);
        }

    }
}
